import { _decorator, AudioClip, Component, instantiate, Node, Prefab, Tween, tween, UIOpacity, UITransform, v2, v3, view } from 'cc';
import { YangLeGeYang_GameData } from "../Data/YangLeGeYang_GameData";
import { YangLeGeYang_CardGameState } from "../Data/YangLeGeYang_GameEnum";
import { bdlMgr } from "../../../../Script/Manager/BundleManager_S";
import YangLeGeYang_CardManager from "./YangLeGeYang_CardManager";
import YangLeGeYang_OperatePanel from "../UI/YangLeGeYang_OperatePanel";
import CommonFunction from "../../../../Script/Utils/CommonFunction";
import YangLeGeYang_GameFunction from "./YangLeGeYang_GameFunction";
import YangLeGeYang_Card from "../Game/YangLeGeYang_Card";
import { SSS_Consts } from "../../../../Script/Data/SSS_Consts";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

const { ccclass, property } = _decorator;

// /**LevelManager */
@ccclass('YangLeGeYangLevelManager')
export default class YangLeGeYang_LevelManager extends Component {
        public static Instance: YangLeGeYang_LevelManager;

        @property({ type: Node })
        public LayerNode: Node | null = null;

        LayerList: Node[] = null;
        MapData: YangLeGeYang_Card[] = null;

        onLoad() {
                YangLeGeYang_LevelManager.Instance = this;
        }
        //    // 再次挑战
        start() {
                const windowSize = view.getVisibleSize();
                // log("width=" + windowSize.width + ",height=" + windowSize.height);

                // 1334,1400
                if (windowSize.height > 1400) {
                        this.LayerNode.scale = v3(1, 1, 1);
                }
                else {
                        this.LayerNode.scale = v3(0.8, 0.8, 0.8)   //0.8;
                }
        }
        Init() {
                this.node.active = true;
                this.InitLayer();
                this.InitMap();
        }
        InitLayer() {
                CommonFunction.SetAllChildrenAtive(this.LayerNode, false);
                // 层级初始化
                for (let index = 0; index < YangLeGeYang_GameData.Leve_l_layer_count; index++) {
                        const element = YangLeGeYang_GameData.Leve_l_layer_count[index];
                        const child = CommonFunction.GetFreeChild(this.LayerNode);
                        child.active = true;
                }
                this.LayerList = this.LayerNode.children;
                // console.log(`this.LayerList.length----------`, this.LayerList.length);
        }
        async InitMap() {
                YangLeGeYang_CardManager.Shuffle_MapData_CurrentLevel();
                // console.log(`DanLeGeCi_GameData.MapData_CurrentLevel-------`, DanLeGeCi_GameData.MapData_CurrentLevel);

                // return
                for (let index = 0; index < YangLeGeYang_GameData.MapData_CurrentLevel.length; index++) {
                        const item_MapData = YangLeGeYang_GameData.MapData_CurrentLevel[index];
                        item_MapData.cardGameState = YangLeGeYang_CardGameState.游戏场景;
                        item_MapData.list_index = index;
                        // console.log(item_MapData);

                        const prefab = await bdlMgr.getRes<Prefab>(`YangLeGeYang_Prefab`, `Card`, Prefab);
                        const node: Node = instantiate(prefab);

                        // console.log(`node`, node);

                        node.active = true;

                        // node.active = false;

                        const card: YangLeGeYang_Card = node.getComponent(YangLeGeYang_Card);
                        card.main.getComponent(UIOpacity).opacity = 0;

                        // card.Init(_index);
                        const card_type = item_MapData.card_type;
                        const card_layer = item_MapData.card_layer;
                        const card_pos = item_MapData.card_pos;
                        card.card_layer = card_layer;
                        // console.log(`card_type---`, card_type);
                        card.node.parent = this.LayerList[card_layer - 1];
                        const x = card_pos.x * YangLeGeYang_GameData.CellWidth;
                        const y = card_pos.y * (YangLeGeYang_GameData.CellHeight2);
                        // console.log(x, y);
                        card.node.setPosition(v3(x, y));
                        card.node.getComponent(UITransform).width = YangLeGeYang_GameData.CellWidth;
                        card.node.getComponent(UITransform).height = YangLeGeYang_GameData.CellHeight;
                        card.list_index = index;

                        let isCover = YangLeGeYang_GameFunction.GetCardCover(item_MapData, YangLeGeYang_GameData.MapData_CurrentLevel);
                        card.isCover = isCover;

                        item_MapData.oldInfo =
                        {
                                card_pos: card_pos,
                                cardGameState: item_MapData.cardGameState,
                        }

                        card.Init(card_type);

                        YangLeGeYang_CardManager.Instance.CardList.push(card);
                }

                YangLeGeYang_GameFunction.RefreshAllCardCover(YangLeGeYang_GameData.MapData_CurrentLevel);


                this.Card开场动画();
        }
        async Card开场动画() {
                let array = YangLeGeYang_CardManager.Instance.CardList;
                for (let index = 0; index < array.length; index++) {
                        const card = array[index];
                        // console.log(`card`, card);

                        this.scheduleOnce(() => {
                                if (card && card.node) {
                                        card.main.getComponent(UIOpacity).opacity = 255;

                                        card.main.scale = v3(0, 0, 0);
                                        let target = card.main;
                                        Tween.stopAllByTarget(target);
                                        tween(target)
                                                .to(0.2, { scale: v3(1.5, 1.5, 1.5) })
                                                .to(0.2, { scale: v3(1, 1, 1) })
                                                .start();

                                        // card.main.active = true;
                                        // card.node.active = true;

                                        // await new Promise(res => SSS_TimerManager.Instance.scheduleOnce(res, 0.008));
                                }
                        }, 0.008 * index);

                }


                let audio_id;
                let audio_root = `audio/life received`;
                bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
                        SSS_AudioManager.Instance.playSFX(res, false);
                });
                if (audio_id) {
                        // audioEngine.stopEffect(audio_id);
                }
        }
        ClearCards() {
                for (let index = 0; index < this.LayerNode.children.length; index++) {
                        const layer = this.LayerNode.children[index];
                        CommonFunction.DestroyAllChildren(layer);
                }

                CommonFunction.DestroyAllChildren(YangLeGeYang_OperatePanel.Instance.node);
                CommonFunction.DestroyAllChildren(YangLeGeYang_OperatePanel.Instance.EmptyLand);
        }
}